package com.haixiaoke.saas.house.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.domain.Unit;
import com.haixiaoke.saas.house.mapper.UnitMapper;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.house.service.IUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 单元Service业务层处理
 *
 * @author xiaolong
 * @date 2023-10-17
 */
@Service
public class UnitServiceImpl implements IUnitService {
    @Autowired
    private UnitMapper unitMapper;

    @Autowired
    private IHouseService iHouseService;

    /**
     * 查询单元
     *
     * @param unitId 单元主键
     * @return 单元
     */
    @Override
    public Unit selectUnitByUnitId(String unitId) {
        return unitMapper.selectUnitByUnitId(unitId);
    }

    /**
     * 查询单元列表
     *
     * @param unit 单元
     * @return 单元
     */
    @Override
    public List<Unit> selectUnitList(Unit unit) {
        return unitMapper.selectUnitList(unit);
    }

    /**
     * 新增单元
     *
     * @param unit 单元
     * @return 结果
     */
    @Override
    public int insertUnit(Unit unit) {
        unit.setCreateTime(DateUtils.getNowDate());
        return unitMapper.insertUnit(unit);
    }

    /**
     * 修改单元
     *
     * @param unit 单元
     * @return 结果
     */
    @Override
    public int updateUnit(Unit unit) {
        Unit byUnitId = unitMapper.selectUnitByUnitId(unit.getUnitId());
        if (!TenantContext.getTenantId().equals(byUnitId.getTenantId())) {
            return 0;
        }

        unit.setUpdateTime(DateUtils.getNowDate());
        return unitMapper.updateUnit(unit);
    }

    /**
     * 批量删除单元
     *
     * @param unitIds 需要删除的单元主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteUnitByUnitIds(String[] unitIds) {
        Unit unit = new Unit();
        unit.setTenantId(TenantContext.getTenantId());
        unit.setUnitIds(unitIds);
        List<Unit> unitList = unitMapper.selectUnitByUnitIds(unit);
        if (unitList.size() < unitIds.length) {
            return 0;
        }

        int i = 0;
        for (String unitId : unitIds) {
            House house = new House();
            house.setUnitId(unitId);
            List<House> houses = iHouseService.selectHouseList(house);
            if (CollectionUtil.isNotEmpty(houses)) {
                throw new ServiceException(houses.get(0).getUnitName() + "单元存在房间不可删除");
            }
            Unit unit1 = new Unit();
            unit1.setUpdateTime(DateUtils.getNowDate());
            unit1.setDelFlag(Unit.DEL_FLAG_NO);
            unit1.setUnitId(unitId);
            unitMapper.updateUnit(unit1);
            i += 1;
        }
        return i;
    }

    /**
     * 删除单元信息
     *
     * @param unitId 单元主键
     * @return 结果
     */
    @Override
    public int deleteUnitByUnitId(String unitId) {
        return unitMapper.deleteUnitByUnitId(unitId);
    }

    @Override
    public Unit selectUnitByUnitBuildingAndUnitName(String buildingId, String unitName) {
        return unitMapper.selectUnitByUnitBuildingAndUnitName(buildingId, unitName);
    }

    @Override
    public Integer selectUnitByUnitNumber(String buildingId, String delFlag, String tenantId) {
        return unitMapper.selectUnitByUnitNumber(buildingId, delFlag, tenantId);
    }
}
